package thinkingInJava.concurrency;

//: concurrency/DaemonFromFactory.java
//Using a Thread Factory to create daemons.
import java.util.concurrent.*;
import static thinkingInJava.YTool.YPrint.*;

public class DaemonFromFactory implements Runnable {
	public void run() {
		try {
			while(true) {
				TimeUnit.MILLISECONDS.sleep(100);
				print(Thread.currentThread() + " " + this);
			}
		} catch(InterruptedException e) {
			print("Interrupted");
		}
	}
	public static void main(String[] args) throws Exception {
		ExecutorService exec = Executors.newCachedThreadPool(
				new DaemonThreadFactory()); // DaemonThreadFactory 只修改线程属性
		for(int i = 0; i < 10; i++)
			exec.execute(new DaemonFromFactory());
		print("All daemons started");
		TimeUnit.MILLISECONDS.sleep(500); // Run for a while
	}
} /* (Execute to see output) *///:~
